import sqlite3
import re


def func(sentence):
    a = []
    
    if sentence:
        words = re.findall(r'[a-zA-Z]+', sentence)
        words = [word.lower() for word in words]
        a = getRootWords(words)
    else:
        a = ''
    
    a = list(set(a))
    return a

def getRootWords(words):
    db = sqlite3.connect("Lemmatization.db")
    roots = db.execute("""
        SELECT IFNULL(paraphrase, entry) AS root
        FROM mdx
        WHERE entry IN ({})
    """.format(','.join(['?'] * len(words))), words).fetchall()
    
    entries = db.execute("""
        SELECT entry
        FROM mdx
        WHERE entry IN ({})
    """.format(','.join(['?'] * len(words))), words).fetchall()
    
    entryVals = [row[0] for row in entries]
    
    words = [word for word in words if word not in entryVals]
    
    allRoots = [row[0] for row in roots]
    
    return allRoots + words

file = open("input.txt", mode="r", encoding="utf-8")
content = file.read()

a=func(content)

with open("out.txt", 'w') as file:
        for item in a:
            file.write(str(item) + ',')
        file.write("\n")
